package com.topvision.camerasdk.album.adapter;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.topvision.camerasdk.R;
import com.topvision.camerasdk.camera.CameraActivity;
import com.topvision.camerasdk.camera.CameraConstants;
import com.topvision.camerasdk.camera.util.CameraUtil;
import com.topvision.camerasdk.video.util.VideoConstant;
import com.topvision.commomsdk.commom.Album;
import com.topvision.commomsdk.uitl.AndroidUtils;
import com.topvision.commomsdk.uitl.DateUtil;
import com.topvision.commomsdk.view.TvGridView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;


public class PhotoGridAdapter extends BaseAdapter {
    private Context mContext;
    private ArrayList<Album> list;
    private int positionTag = 0;
    public static final String CURRENT_POSITON = "CURRENT_POSITON";
    public static final String MEDIA_URL = "MEDIA_URL";
    /*****
     * 12.13
     *****/
    private String ablumType;
    boolean voiceTipFlag;

    /*****
     * End
     *****/
    public void updateData(ArrayList<Album> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public PhotoGridAdapter(Context mContext, ArrayList<Album> list, String ablumType, boolean voiceTipFlag) {

        super();
        this.mContext = mContext;
        this.list = list;
        this.ablumType = ablumType; /***** 12.13 修改 **/
        this.voiceTipFlag = voiceTipFlag;
    }

    public void upData(int positionTag) {
        this.positionTag = positionTag;
        notifyDataSetChanged();
    }

    public void upDataList(int positionTag, ArrayList<Album> list) {
        this.list = list;
        this.positionTag = positionTag;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Album getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.activity_camera_grid_item, parent, false);
        }

        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) mContext).getWindowManager().getDefaultDisplay().getMetrics(dm);
        TvGridView.LayoutParams params = null;
        float voiceTips = mContext.getResources().getDimension(R.dimen.voice_tips_height);
        //如果有语音提示显示的话,需要考虑到其高度
        params = new TvGridView.LayoutParams(dm.widthPixels / 4, (dm.heightPixels - (int) voiceTips) / 2);
//        if (voiceTipFlag) {
//        } else {
//            params = new TvGridView.LayoutParams(dm.widthPixels / 4, (dm.heightPixels) / 2);
//        }
        convertView.setLayoutParams(params);


        ImageView iv = BaseViewHolder.get(convertView, R.id.iv_item);
        TextView tv_tag = BaseViewHolder.get(convertView, R.id.tv_tag);
        ImageView videoIv = BaseViewHolder.get(convertView, R.id.video_iv);
        TextView tv_time = BaseViewHolder.get(convertView, R.id.tv_time);

        /*****12.13  *****/
        if (ablumType.equals(VideoConstant.ABLUM_TYPE_MEDIA)) {
            Album album = list.get(position);
            File file = album.getFile();
            String mp4Name = file.getName();

            String path = file.getPath();
//            String jpgpath = path.replace("mp4", "jpg");
            String thumbnailPath = CameraConstants.THUMBNAIL_FOLDER + mp4Name.replace(".mp4", "-video.jpg");
            if (CameraUtil.fileIsExists(thumbnailPath)) {
                File jpgFile = new File(thumbnailPath);
                iv.setTag(path);// 绑定imageview
                Picasso.with(mContext).load(jpgFile).centerCrop().resize(dm.widthPixels / 4, (dm.heightPixels - (int) voiceTips) / 2).error(R.mipmap.pic_fail).into(iv);
//                if(voiceTipFlag){
//                }else{
//                    Picasso.with(mContext).load(jpgFile).centerCrop().resize(dm.widthPixels / 4, dm.heightPixels / 2).error(R.mipmap.pic_fail).into(iv);
//                }
            } else {
                //如果视频没有缩略图 就创建保存
                Bitmap videoBitmap = CameraUtil.getVidioBitmap(path, CameraActivity.DEFAULT_PICTURE_WIDTH, CameraActivity.DEFAULT_PICTURE_WIDTH, MediaStore.Images.Thumbnails.MICRO_KIND);
//                String bitName = mp4Name.replace("mp4", "jpg");
                saveMyBitmap(videoBitmap, thumbnailPath);
//                String jpgPath = path.replace("mp4", "jpg");
                File jpgFile = new File(thumbnailPath);
                iv.setTag(path);// 绑定imageView
                Picasso.with(mContext).load(jpgFile).centerCrop().resize(dm.widthPixels / 4, (dm.heightPixels - (int) voiceTips) / 2).error(R.mipmap.pic_fail).into(iv);
//                if(voiceTipFlag){
//                }else{
//                    Picasso.with(mContext).load(jpgFile).centerCrop().resize(dm.widthPixels / 4, dm.heightPixels / 2).error(R.mipmap.pic_fail).into(iv);
//                }
            }
            try {
                long time = Long.parseLong(album.getTime());
                tv_time.setText(DateUtil.transformHMS(time));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (ablumType.equals(VideoConstant.ABLUM_TYPE_PHOTO)) {
            videoIv.setVisibility(View.GONE);
            tv_time.setVisibility(View.GONE);
            Album album = list.get(position);
            String thumbnailPath = album.getThumbnailPath();
            if (thumbnailPath.equals("document")) {
                String path = album.getPath();
                int resourceId = R.mipmap.pic_fail;
                if (path.endsWith(".doc")) {
                    resourceId = R.mipmap.mdoc;
                } else if (path.endsWith(".xls")) {
                    resourceId = R.mipmap.xls;
                } else if (path.endsWith(".pdf")) {
                    resourceId = R.mipmap.pdf;
                }
                Picasso.with(mContext).load(resourceId).centerCrop().error(R.mipmap.pic_fail).into(iv);
            } else {
                File file = new File(thumbnailPath);
                if (voiceTipFlag) {
                    Picasso.with(mContext).load(file).centerCrop().resize(dm.widthPixels / 4, (dm.heightPixels - (int) voiceTips) / 2).error(R.mipmap.pic_fail).into(iv);
                } else {
                    Picasso.with(mContext).load(file).centerCrop().resize(dm.widthPixels / 4, dm.heightPixels / 2).error(R.mipmap.pic_fail).into(iv);
                }

            }

        }
        /*****  End *****/
        if (AndroidUtils.isBtDevice()) {
            if (backGroundFlag) {
                //显示选择背景
                showSelectPosition(tv_tag, position);
            } else {
                //隐藏选择背景
                tv_tag.setVisibility(View.GONE);
            }
        } else {
            showSelectPosition(tv_tag,position);
        }
        return convertView;
    }

    private void showSelectPosition(TextView tv_tag,int position) {
        if (positionTag == position) {
            tv_tag.setVisibility(View.VISIBLE);
        } else {
            tv_tag.setVisibility(View.GONE);
        }
    }

    public void saveMyBitmap(Bitmap mBitmap, String path) {
//        File f = new File(MediaRecorderActivity.TAKE_VIDEO_FOLDER + bitName);
        File f = new File(path);
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (mBitmap != null) {
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        }
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ArrayList<Album> getList() {
        return list;
    }

    public void setList(ArrayList<Album> list) {
        this.list = list;
    }

    private boolean backGroundFlag;
    public void hideBackGround() {
        backGroundFlag = false;
        notifyDataSetChanged();
    }

    public void showBackGround() {
        backGroundFlag = true;
        notifyDataSetChanged();
    }
}
